home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 60750 / 60750.xpi / surfcanyon_sc.xpi / content / xmlhttprequester.js < prev   
Text File  |  2009-11-12  |  2KB  |  81 lines

  1. function surfcanyon_xmlhttpRequester(unsafeContentWin, chromeWindow) {
  2.     this.unsafeContentWin = unsafeContentWin;
  3.     this.chromeWindow = chromeWindow;
  4. }
  5.  
  6. // this function gets called by user scripts in content security scope to
  7. // start a cross-domain xmlhttp request.
  8. //
  9. // details should look like:
  10. // {method,url,onload,onerror,onreadystatechange,headers,data}
  11. // headers should be in the form {name:value,name:value,etc}
  12. surfcanyon_xmlhttpRequester.prototype.contentStartRequest = function(details) {
  13.     var url = details.url;
  14.     this.chromeWindow.setTimeout(
  15.         surfcanyon_gmCompiler.hitch(this, "chromeStartRequest", url, details), 0);
  16. }
  17.  
  18. // this function is intended to be called in chrome's security context, so
  19. // that it can access other domains without security warning
  20. surfcanyon_xmlhttpRequester.prototype.chromeStartRequest=function(url, details) {
  21.     var req = new this.chromeWindow.XMLHttpRequest();
  22.  
  23.     this.setupRequestEvent(this.unsafeContentWin, req, "onload", url, details);
  24.     this.setupRequestEvent(this.unsafeContentWin, req, "onerror", url, details);
  25.     this.setupRequestEvent(this.unsafeContentWin, req, "onreadystatechange", url, details);
  26.  
  27.     req.open(details.method, url);
  28.     
  29.     if (details.mimeType) {
  30.         req.overrideMimeType(details.mimeType);
  31.     }
  32.  
  33.     if (details.headers) {
  34.         for (var prop in details.headers) {
  35.             req.setRequestHeader(prop, details.headers[prop]);
  36.         }
  37.     }
  38.  
  39.     req.send(details.data);
  40. }
  41.  
  42. // arranges for the specified 'event' on xmlhttprequest 'req' to call the
  43. // method by the same name which is a property of 'details' in the content
  44. // window's security context.
  45. surfcanyon_xmlhttpRequester.prototype.setupRequestEvent =
  46. function(unsafeContentWin, req, event, url, details) {
  47.     if (details[event]) {
  48.         req[event] = function() {
  49.             var responseHeaders = '';
  50.             var status = 0;
  51.             var statusText = '';
  52.  
  53.             if (req.readyState == 4) {
  54.                 try {
  55.                     responseHeaders = req.getAllResponseHeaders();
  56.                     status = req.status;
  57.                     statusText = req.statusText;
  58.                 } catch (e) {
  59.                 }
  60.             }
  61.  
  62.             var responseState = {
  63.                 url: url,
  64.                 responseText: req.responseText,
  65.                 readyState: req.readyState,
  66.                 responseHeaders: responseHeaders,
  67.                 status: status,
  68.                 statusText: statusText
  69.             }
  70.  
  71.             // Pop back onto browser thread and call event handler.
  72.             new XPCNativeWrapper(unsafeContentWin, "setTimeout()").setTimeout(
  73.                 function() {
  74.                     details[event](responseState);
  75.                 },
  76.                 0);
  77.         }
  78.     }
  79. }
  80.  
  81.